package adapter;

import java.util.ArrayList;

import com.adgroup.smartnote.R;
import com.androidquery.AQuery;

import JsonModel.Category;
import JsonModel.Spending;
import adapter.AdapterUserLogo.Holder;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class AdapterCreateNoteExpense{

	ArrayList<Spending> arr;
	Context context;
	//int miss, idMiss;
	int [] miss;
	public AdapterCreateNoteExpense(Context c, ArrayList<Spending> arr)
	{
		this.arr = arr;
		context = c;
	}
	public class Holder
	{
		public EditText etAmount, etDescription, etPrice;
	}
	public class Adapter extends BaseAdapter
	{

		@Override
		public int getCount() {
			return arr.size();
		}

		@Override
		public Object getItem(int position) {
			return arr.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}
		public int addItem()
		{
			if(arr.size()>0){
				Spending s = arr.get(arr.size()-1);
				if(s.getAmount().equalsIgnoreCase("")){
					return 1;
				}
				else if(s.getDescription().equalsIgnoreCase("")){
					return 2;
				}
				else if(s.getPrice().equalsIgnoreCase("")){
					return 3;
				}
			}
			arr.add(new Spending());
			this.notifyDataSetChanged();
			return 0;
		}
		@Override
		public View getView(final int position, View arg1, ViewGroup parent) {
			final Holder holder;
			LayoutInflater inflater = ((Activity) context).getLayoutInflater();
			if (arg1 == null) {
				holder = new Holder();
				arg1 = inflater.inflate(R.layout.item_createnote_homekeeper, null);
				holder.etAmount = (EditText) arg1
						.findViewById(R.id.editTextAmountExpense);
				holder.etDescription = (EditText) arg1
						.findViewById(R.id.editTextItemsExpense);
				holder.etPrice = (EditText) arg1
						.findViewById(R.id.editTextPriceExpense);
				
				arg1.setTag(holder);
			} else {
				holder = (Holder) arg1.getTag();
			}
			holder.etAmount.setOnFocusChangeListener(new OnFocusChangeListener() {
				
				@Override
				public void onFocusChange(View arg0, boolean arg1) {
					if(!arg1){
						arr.get(position).setAmount(holder.etAmount.getText().toString());
					}
				}
			});
			holder.etDescription.setOnFocusChangeListener(new OnFocusChangeListener() {
							
							@Override
							public void onFocusChange(View arg0, boolean arg1) {
								if(!arg1){
									arr.get(position).setDescription(holder.etDescription.getText().toString());
								}
							}
						});
			holder.etPrice.setOnFocusChangeListener(new OnFocusChangeListener() {
				
				@Override
				public void onFocusChange(View arg0, boolean arg1) {
					if(!arg1){
						arr.get(position).setPrice(holder.etPrice.getText().toString());
					}
				}
			});		
			//set show key board to missing
			if(miss!=-1 && idMiss!=-1){
				
			}
			return arg1; 
		}

		public boolean checkValidItem() {
			for (int i = 0; i < arr.size(); i++) {
				if(arr.get(i).getAmount().equalsIgnoreCase("")){
					miss = i;
					idMiss = 1;
					this.notifyDataSetChanged();
					return false;
				}
				else if(arr.get(i).getAmount().equalsIgnoreCase("")){
					miss = i;
					idMiss = 2;
					this.notifyDataSetChanged();
					return false;
				}
				else if(arr.get(i).getAmount().equalsIgnoreCase("")){
					miss = i;
					idMiss = 3;
					this.notifyDataSetChanged();
					return false;
				} 
			}
			miss = idMiss = -1;
			return true;
			
		}
		
	}
}
